<?php
namespace classes;
class Mysql {
	//声明常量
	const MYSQL_NUM = MYSQLI_NUM; //索引数组
	const MYSQL_ASSOC = MYSQLI_ASSOC; //关联数组
	const MYSQL_BOTH = MYSQLI_BOTH; //两者兼有
	//用于存储连接的对象,以谋求在各个方法内都可以被访问到
	protected $_link; 
	//用于设置匹配的数组的类型
	protected $_fetchMode = Mysql::MYSQL_ASSOC;
	//构造函数
	public function __construct($hostname, $username, $password, $database) {
		$this->_link = mysqli_connect ( $hostname, $username, $password, $database );	
	}

	//设置_fetchMode属性的值
	public function setFetchMode($mode){
		if($mode != self::MYSQL_NUM && $mode != self::MYSQL_ASSOC && $mode != self::MYSQL_BOTH){
			$mode = self::MYSQL_ASSOC;
		}
		$this->_fetchMode = $mode;
	}
	public function getLink(){
		return $this->_link;
	}
	public function insert($table, $bind) {
		$cols = join ( ',', array_keys ( $bind ) );
		$vals = join ( '\',\'', $bind );
		$sql = 'INSERT ' . $table . '(' . $cols . ') VALUES(\'' . $vals . '\')';
		mysqli_query ( $this->_link, $sql );
		return mysqli_affected_rows ( $this->_link );
	}
	/**
	 * 构造标准的SQL SELECT语句
	 * @access protected
	 * @param  string $table,数据表名称
	 * @param  string $config,SELECT语句的配置选项,包含:
	 * fields,
	 * where,
	 * group,
	 * having,
	 * order,
	 * limit, 
	 * @return string,标准的SQL SELECT语句的字符串
	 */
	protected function _buildSQL($table,$config){
		if(!isset($config['fields'])){
			$config['fields']  = '*';
		}
		$sql = 'SELECT ' . $config['fields'] . ' FROM ' . $table;
		if(isset($config['where'])){
		
			$sql .= ' WHERE ' . $config['where'];
		}
		if(isset($config['group'])){
			$sql .= ' GROUP BY ' . $config['group'];
		}
		if(isset($config['having'])){
			$sql .= ' HAVING ' . $config['having'];
		}
		if(isset($config['order'])){
			$sql .= ' ORDER BY ' . $config['order'];
		}
		if(isset($config['limit'])){
			$sql .= ' LIMIT ' . $config['limit'];
		}
		return $sql;
	}
	/**
	 * 抽取"所有"记录 
	 * @param string $table,数据表名称
	 * @param array $config,相关的配置选项
	 * @return array,由所有的记录组成二维数组????
	 */
	public function fetchAll($table,$config=array()){
		$sql = $this->_buildSQL($table,$config);
		$result = mysqli_query($this->_link, $sql); //结果集(可能为空集)
		$rowsCount = mysqli_num_rows($result); //结果集包含的行数(>=0的整数)
		if(!$rowsCount){
			return false;
		}
		//rowset,行集合
		
		$rowset = mysqli_fetch_all($result,$this->_fetchMode);
		return $rowset;
		
	}
	
	/**
	 * 抽取一条记录
	 * @param string $table,数据表名称
	 * @param array $config,查找时相关的配置选项
	 * @return array,记录的相关信息组成的数组(????)
	 */
	public function fetch($table,$config=array()){
		$sql = $this->_buildSQL($table,$config);				
		$result = mysqli_query($this->_link, $sql); //结果集(可能为空集)
		$rowsCount = mysqli_num_rows($result); //结果集包含的行数(>=0的整数)
		if(!$rowsCount){
			return false;
		}
		//从结果集中抽取一条记录作为关联数组返回
		return mysqli_fetch_array($result,$this->_fetchMode);
	}	

	function delete($table,$where=null){
		$sql = 'DELETE FROM ' . $table . (is_null($where) ? $where : ' WHERE ' . $where);
		mysqli_query($this->_link,$sql);
		return mysqli_affected_rows($this->_link);
	}
	function update($table,$bind,$where=null){
		$string = null;
		$sql = 'UPDATE ' . $table . ' SET ';
		foreach($bind as $col => $val){
			$string .= (is_null($string) ? null : ',') . $col . '=\'' . $val . '\'';
		}		
		$sql .= $string .  (is_null($where) ? $where : ' WHERE ' . $where);
		mysqli_query($this->_link,$sql);
		return mysqli_affected_rows($this->_link);
	}
	function __destruct(){
		mysqli_close($this->_link);		
	}
	
}